package es.cazadoresmadridejos.beans.manager.deer.impl;

import java.util.ArrayList;
import java.util.List;

import org.displaytag.pagination.PaginatedList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import es.cazadoresmadridejos.beans.dao.deer.DeerDao;
import es.cazadoresmadridejos.beans.manager.deer.DeerManager;
import es.cazadoresmadridejos.common.bean.dao.Dao;
import es.cazadoresmadridejos.common.bean.manager.impl.ManagerImpl;
import es.cazadoresmadridejos.model.Deer;
import es.cazadoresmadridejos.objs.web.Page;

@Service
public class DeerManagerImpl extends ManagerImpl<Deer> 
implements DeerManager{

	@Autowired 
	public void setDao(@Qualifier("deerDao")
	Dao<Deer> dao) {
		this.dao = dao;
	}

	@Override
	public List<Deer> findAllAsistances(Long id) {
		return ((DeerDao)dao).findAllAssitances(id);
	}

	@Transactional(rollbackFor=Exception.class)
	@Override
	public void saveAll(List<Deer> list, Long reasonid) {
		for (Deer deer : list) {
			save(deer);
		}

		this.deleteNotIn(list,reasonid);
	}

	private void deleteNotIn(List<Deer> list, Long reasonid) {
		List<Long> deerIds = new ArrayList<Long>();
		for (Deer deer : list) {
			if(deer.getId() != null){
				deerIds.add(deer.getId());
			}
		}
		((DeerDao)dao).deleteNotIn(deerIds, reasonid);
	}

	@Override
	public PaginatedList findAllPaginated(Page page, Long id) {
		return ((DeerDao)dao).findAllPaginated(page,id);
	}

}
